# main.py

import sys
import os
import json
import logging
from PyQt5.QtWidgets import QApplication
from views.main_window import MainWindow

# 配置日志
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(levelname)s - %(message)s',
    handlers=[
        logging.FileHandler("image_viewer.log", encoding='utf-8'),
        logging.StreamHandler(sys.stdout)
    ]
)

def main():
    # 检查设置文件是否存在，如果不存在则创建默认设置
    if not os.path.exists('settings.json'):
        with open('settings.json', 'w', encoding='utf-8') as f:
            json.dump({"directory": "", "favorites": [], "auto_update": False}, f, ensure_ascii=False, indent=4)
        logging.info("默认设置文件已创建。")

    app = QApplication(sys.argv)

    # 启用OpenGL硬件加速（如果可用）
    from PyQt5.QtGui import QSurfaceFormat
    format = QSurfaceFormat()
    format.setDepthBufferSize(24)
    format.setStencilBufferSize(8)
    format.setVersion(3, 3)
    format.setProfile(QSurfaceFormat.CoreProfile)
    QSurfaceFormat.setDefaultFormat(format)

    # 检查OpenGL是否成功启用
    if QSurfaceFormat.defaultFormat().profile() == QSurfaceFormat.CoreProfile:
        logging.info("OpenGL硬件加速已启用。")
    else:
        logging.warning("未能启用OpenGL硬件加速。")

    window = MainWindow()
    window.show()
    sys.exit(app.exec())

if __name__ == "__main__":
    main()
